package com.rdgk.newtech.service.impl;

import com.rdgk.newtech.dao.*;
import com.rdgk.newtech.service.NewTechAnalyseService;
import com.rdgk.newtech.service.OrgAnalyseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.event.ObjectChangeListener;
import java.util.*;

@Service
public class OrgAnalyseServiceImpl implements OrgAnalyseService {
    @Autowired
    private TechniqueArticleDao techArticleDao;
    @Autowired
    private PaperDao paperDao;
    @Autowired
    private PatentDao patentDao;
    @Autowired
    private CraOrganizationDao organizationDao;
    @Autowired
    private TechniqueWordDao techniqueWordDao;
    @Autowired
    private CraAuthorDao authorDao;

    @Override
    public List<Map<String,Object>> importantOrgList(Integer topicId, String[] words) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        return organizationDao.getImportantOrgs(params);
    }

    @Override
    public List<Map<String,Object>> importantOrgList(Integer topicId, String[] words,String count) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("count",count);
        return organizationDao.getImportantOrgs(params);
    }

    @Override
    public List<Map<String,Object>> technologyRegionalDistribution(Integer topicId, String[] words) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        return organizationDao.statisticsOrgNumForMap(params);
    }

    @Override
    public Map<String, Object> allOrgCooperateNet(Integer topicId, String keyword) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("keyword",keyword);
        Map<String,Object> results = new HashMap<String,Object>();
        List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();//节点列表
        List<Map<String,Object>> edges = new ArrayList<Map<String,Object>>();//关系列表

        List<Map<String,Object>> organizationNodes = new ArrayList<Map<String,Object>>();//机构节点

        List<Map<String,Object>> orgEdges = new ArrayList<Map<String,Object>>();//机构与机构关系

        Map<Object,List<Object>> relatedOrgList = new HashMap<Object,List<Object>>();

        Map<String,Integer> relIndex = new HashMap<String,Integer>();//记录关系元素在关系列表中的下标
        List<Map<String,Object>> relOrgs = organizationDao.getOrgCooperateNet(params);
        List<Object> tmpOrgIds = new ArrayList<Object>();//临时存放机构id的list，用于去重
        for (Map<String,Object> relorg:relOrgs
             ) {
            Object orgid = relorg.get("coid");
            if(!tmpOrgIds.contains(orgid)){
                Map<String,Object> orgNode = new HashMap<String,Object>();//创建机构节点
                orgNode.put("id","org_"+orgid);
                orgNode.put("label",relorg.get("coname"));
                organizationNodes.add(orgNode);
                tmpOrgIds.add(orgid);//标记该机构已经出现过
            }
            Object articleid = relorg.get("id")+"_"+relorg.get("type");//与机构相关的新闻动态、论文、专利的id
            List<Object> orgList = relatedOrgList.get(articleid);
            if(orgList==null||orgList.size()<=0){
                orgList = new ArrayList<Object>();
                orgList.add(orgid);
            }else{
                if(!orgList.contains(orgid))
                    orgList.add(orgid);
            }
            relatedOrgList.put(articleid,orgList);
        }
        Set<Object> articleKeys = relatedOrgList.keySet();
        for (Object articleKey:articleKeys
                ) {//给热词节点的size属性赋值
            List<Object> orgList = relatedOrgList.get(articleKey);
            if(orgList==null)
                continue;
            Object orgid = orgList.get(0);
            for (int i = 1;i<orgList.size();i++){
                if(relIndex.get(orgid+"rel"+orgList.get(i))==null&&relIndex.get(orgList.get(i)+"rel"+orgid)==null){
                    Map<String,Object> orgEdge = new HashMap<String,Object>();
                    orgEdge.put("sourceID", "org_" + orgid);
                    orgEdge.put("targetID", "org_" + orgList.get(i));
                    orgEdge.put("size", 1);
                    relIndex.put(orgid+"rel"+orgList.get(i),orgEdges.size());
                    orgEdges.add(orgEdge);
                }else{
                    Integer index = (relIndex.get(orgList.get(i)+"rel"+orgid)==null)?relIndex.get(orgid+"rel"+orgList.get(i)):relIndex.get(orgList.get(i)+"rel"+orgid);
                    Map<String,Object> orgEdge = orgEdges.get(index);
                    Integer size = (Integer)orgEdge.get("size");
                    orgEdge.put("size", ++size);
                }
            }
        }
        //将所有节点增加到nodes集合中
        nodes.addAll(organizationNodes);
        edges.addAll(orgEdges);
        //将关系和节点列表添加到结果集中
        results.put("nodes",nodes);
        results.put("edges",edges);
        return results;
    }

    private void computeSize(Map<Object,Integer> nodeSize,Object nodeId){
        Integer size = nodeSize.get(nodeId);
        nodeSize.put(nodeId,size==null?1:++size);
    }

    @Override
    public  Map<String,Object>  orgDetails(Integer topicId, Integer orgId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("orgId",orgId);
        Map<String,Object> results = new HashMap<String,Object>();

        List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();//节点列表
        List<Map<String,Object>> edges = new ArrayList<Map<String,Object>>();//关系列表

        List<Map<String,Object>> orgNodes = new ArrayList<Map<String,Object>>();//网络新闻动态节点
        List<Map<String,Object>> orgEdges = new ArrayList<Map<String,Object>>();//机构与机构关系

        List<Object> articleIdParams = new ArrayList<Object>();//用于传递参数的网络新闻动态id的集合
        List<Object> paperIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        List<Object> patentIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        List<Object> orgIdParams = new ArrayList<Object>();//用于传递参数的网络新闻动态id的集合

        List<Map<String,Object>> techNums = new ArrayList<Map<String,Object>>();//技术动态统计数据

        Map<Object,List<Object>> relatedOrgList = new HashMap<Object,List<Object>>();
        Map<String,Integer> relIndex = new HashMap<String,Integer>();//记录关系元素在关系列表中的下标
        List<Map<String,Object>> orginfos = organizationDao.getOrgByID(params);
        Map<String,Object> orgdetail = orginfos.size()>0?orginfos.get(0):null;//机构详情
        Map<String,Object> orgNode = new HashMap<String,Object>();
        orgNode.put("id","org_"+orgdetail.get("id"));
        orgNode.put("label",orgdetail.get("NAME"));
        orgNodes.add(orgNode);
        orgIdParams.add(orgdetail.get("id"));

        List<Map<String,Object>> hotworddata = new ArrayList<Map<String,Object>>();//机构相关热词
        hotworddata = organizationDao.getOrgHotwords(params);

        List<Map<String,Object>> authors = new ArrayList<Map<String,Object>>();//机构相关专家
        authors = organizationDao.getOrgAuthors(params);

        List<Map<String,Object>> orgTechEvents = new ArrayList<Map<String,Object>>();//机构相关技术动态

        orgTechEvents = organizationDao.getOrgTechEvents(params);
        for (Map<String,Object> orgTechEvent:orgTechEvents
             ) {
            if("0".equals(orgTechEvent.get("reltype"))){//创建专家与论文关系
                if(!articleIdParams.contains(orgTechEvent.get("id"))){
                    articleIdParams.add(orgTechEvent.get("id"));
                }
                Object articleid = orgTechEvent.get("id")+"_0";//与机构相关的新闻动态、论文、专利的id
                Object orgid = orgdetail.get("id");//与机构相关的新闻动态、论文、专利的id
                List<Object> orgList = relatedOrgList.get(articleid);
                if(orgList==null||orgList.size()<=0){
                    orgList = new ArrayList<Object>();
                    orgList.add(orgid);
                }else{
                    if(!orgList.contains(orgid))
                        orgList.add(orgid);
                }
                relatedOrgList.put(articleid,orgList);
            }
            if("1".equals(orgTechEvent.get("reltype"))){//创建专家与论文关系
                Map<String,Object> paperNode = new HashMap<String,Object>();
                if(!paperIdParams.contains(orgTechEvent.get("id"))) {
                    paperIdParams.add(orgTechEvent.get("id"));
                }
                Object articleid = orgTechEvent.get("id")+"_1";//与机构相关的新闻动态、论文、专利的id
                Object orgid = orgdetail.get("id");//与机构相关的新闻动态、论文、专利的id
                List<Object> orgList = relatedOrgList.get(articleid);
                if(orgList==null||orgList.size()<=0){
                    orgList = new ArrayList<Object>();
                    orgList.add(orgid);
                }else{
                    if(!orgList.contains(orgid))
                        orgList.add(orgid);
                }
                relatedOrgList.put(articleid,orgList);
            }
            if("2".equals(orgTechEvent.get("reltype"))) {//创建专家与专利关系{
                Map<String, Object> patentNode = new HashMap<String, Object>();
                if(!patentIdParams.contains(orgTechEvent.get("id"))) {
                    patentIdParams.add(orgTechEvent.get("id"));
                }
                Object articleid = orgTechEvent.get("id")+"_2";//与机构相关的新闻动态、论文、专利的id
                Object orgid = orgdetail.get("id");//与机构相关的新闻动态、论文、专利的id
                List<Object> orgList = relatedOrgList.get(articleid);
                if(orgList==null||orgList.size()<=0){
                    orgList = new ArrayList<Object>();
                    orgList.add(orgid);
                }else{
                    if(!orgList.contains(orgid))
                        orgList.add(orgid);
                }
                relatedOrgList.put(articleid,orgList);
            }
        }

        params.put("relArticleList",articleIdParams);
        params.put("relPaperList",paperIdParams);
        params.put("relPatentList",patentIdParams);
        List<Map<String,Object>> relatedOrgs =  organizationDao.getOrgRelatedOrgs(params);
        for (Map<String,Object> relatedOrg:relatedOrgs
                ) {
            if("0".equals(relatedOrg.get("reltype"))){//创建专家与论文关系
                Map<String,Object> relatedOrgNode = new HashMap<String,Object>();
                if(!orgIdParams.contains(relatedOrg.get("coid"))){
                    relatedOrgNode.put("id","org_"+relatedOrg.get("coid"));
                    relatedOrgNode.put("label",relatedOrg.get("coname"));
                    orgNodes.add(relatedOrgNode);
                    orgIdParams.add(relatedOrg.get("coid"));
                }

                Object articleid = relatedOrg.get("id")+"_0";//与机构相关的新闻动态、论文、专利的id
                Object orgid = relatedOrg.get("coid");//与机构相关的新闻动态、论文、专利的id
                List<Object> orgList = relatedOrgList.get(articleid);
                if(orgList==null||orgList.size()<=0){
                    orgList = new ArrayList<Object>();
                    orgList.add(orgid);
                }else{
                    if(!orgList.contains(orgid))
                        orgList.add(orgid);
                }
                relatedOrgList.put(articleid,orgList);
            }
            if("1".equals(relatedOrg.get("reltype"))){//创建专家与论文关系
                Map<String,Object> relatedOrgNode = new HashMap<String,Object>();
                if(!orgIdParams.contains(relatedOrg.get("coid"))){
                    relatedOrgNode.put("id","org_"+relatedOrg.get("coid"));
                    relatedOrgNode.put("label",relatedOrg.get("coname"));
                    orgNodes.add(relatedOrgNode);
                    orgIdParams.add(relatedOrg.get("coid"));
                }
                Object articleid = relatedOrg.get("id")+"_1";//与机构相关的新闻动态、论文、专利的id
                Object orgid = relatedOrg.get("coid");//与机构相关的新闻动态、论文、专利的id
                List<Object> orgList = relatedOrgList.get(articleid);
                if(orgList==null||orgList.size()<=0){
                    orgList = new ArrayList<Object>();
                    orgList.add(orgid);
                }else{
                    if(!orgList.contains(orgid))
                        orgList.add(orgid);
                }
                relatedOrgList.put(articleid,orgList);
            }
            if("2".equals(relatedOrg.get("reltype"))) {//创建专家与专利关系{
                Map<String,Object> relatedOrgNode = new HashMap<String,Object>();
                if(!orgIdParams.contains(relatedOrg.get("coid"))) {
                    relatedOrgNode.put("id", "org_"+relatedOrg.get("coid"));
                    relatedOrgNode.put("label", relatedOrg.get("coname"));
                    orgNodes.add(relatedOrgNode);
                    orgIdParams.add(relatedOrg.get("coid"));
                }
                Object articleid = relatedOrg.get("id")+"_2";//与机构相关的新闻动态、论文、专利的id
                Object orgid = relatedOrg.get("coid");//与机构相关的新闻动态、论文、专利的id
                List<Object> orgList = relatedOrgList.get(articleid);
                if(orgList==null||orgList.size()<=0){
                    orgList = new ArrayList<Object>();
                    orgList.add(orgid);
                }else{
                    if(!orgList.contains(orgid))
                        orgList.add(orgid);
                }
                relatedOrgList.put(articleid,orgList);
            }
        }

        Set<Object> articleKeys = relatedOrgList.keySet();
        for (Object articleKey:articleKeys
                ) {//给热词节点的size属性赋值
            List<Object> orgList = relatedOrgList.get(articleKey);
            if(orgList==null)
                continue;
            Object orgid = orgList.get(0);
            for (int i = 1;i<orgList.size();i++){
                if(relIndex.get(orgid+"rel"+orgList.get(i))==null&&relIndex.get(orgList.get(i)+"rel"+orgid)==null){
                    Map<String,Object> orgEdge = new HashMap<String,Object>();
                    orgEdge.put("sourceID", "org_" + orgid);
                    orgEdge.put("targetID", "org_" + orgList.get(i));
                    orgEdge.put("size", 1);
                    relIndex.put(orgid+"rel"+orgList.get(i),orgEdges.size());
                    orgEdges.add(orgEdge);
                }else{
                    Integer index = (relIndex.get(orgList.get(i)+"rel"+orgid)==null)?relIndex.get(orgid+"rel"+orgList.get(i)):relIndex.get(orgList.get(i)+"rel"+orgid);
                    Map<String,Object> orgEdge = orgEdges.get(index);
                    Integer size = (Integer)orgEdge.get("size");
                    orgEdge.put("size", ++size);
                }
            }
        }

        //将所有节点增加到nodes集合中
        nodes.addAll(orgNodes);
        //将所有关系增加到edges集合中
        edges.addAll(orgEdges);
        //将关系和节点列表添加到结果集中
        results.put("nodes",nodes);
        results.put("edges",edges);

        results.put("orgdetail",orgdetail);//机构详细信息
        results.put("hotwords",hotworddata);//机构相关热词
        results.put("authors",authors);//机构相关专家

        Map<String,Object> articelNum = new HashMap<String,Object>();
        articelNum.put("key","articelNum");
        articelNum.put("num",articleIdParams.size());
        articelNum.put("text","新闻动态");
        Map<String,Object> paperNum = new HashMap<String,Object>();
        paperNum.put("key","paperNum");
        paperNum.put("num",paperIdParams.size());
        paperNum.put("text","论文");
        Map<String,Object> patentNum = new HashMap<String,Object>();
        patentNum.put("key","patentNum");
        patentNum.put("num",patentIdParams.size());
        patentNum.put("text","专利");

        techNums.add(articelNum);
        techNums.add(paperNum);
        techNums.add(patentNum);

        results.put("techNums",techNums);//机构相关技术动态数量
        return results;
    }
    /**
     * 获取某个机构相关新闻动态信息列表
     * @param  orgId 机构ID
     * @return
     */
    public  List<Map<String,Object>>  orgTechDetails(Integer topicId,Integer orgId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("orgId",orgId);
        return organizationDao.orgTechDetails(params);
    }

    /**
     * 获取某个机构相关论文列表
     * @param  orgId 机构ID
     * @return
     */
    public  List<Map<String,Object>>  orgPaperDetails(Integer topicId,Integer orgId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("orgId",orgId);
        return organizationDao.orgPaperDetails(params);
    }
    /**
     * 获取某个机构相关专利列表
     * @param  orgId 机构ID
     * @return
     */
    public  List<Map<String,Object>>  orgPatentDetails(Integer topicId,Integer orgId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("orgId",orgId);
        return organizationDao.orgPatentDetails(params);
    }

    public  List<Map<String,Object>>  orgPaperDetails(Map<String,Object> params){
        return organizationDao.orgPaperDetails(params);
    }

    public  List<Map<String,Object>>  orgTechDetails(Map<String,Object> params){
        return organizationDao.orgTechDetails(params);
    }
    public  List<Map<String,Object>>  orgPatentDetails(Map<String,Object> params){
        return organizationDao.orgPatentDetails(params);
    }

}
